package clients;

import generated.Adresse;
import generated.Annuaire;
import generated.AnnuaireService;
import generated.ExporterPDFResponse;
import generated.NomInconnuException_Exception;
import generated.Personne;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutionException;

import javax.xml.ws.AsyncHandler;
import javax.xml.ws.Response;

public class Client {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		List<Adresse> adresses = new ArrayList<Adresse>();
		 Annuaire proxy =	new AnnuaireService().getAnnuairePort();
		 Adresse adr1 = new Adresse();
		 adr1.setCodePostal(92400);
		 adr1.setNumero(328);
		 adr1.setVille("Paris");
		 adr1.setVoie("Georges Clemenceau");
		 Adresse adr2 = new Adresse();
		 adr2.setCodePostal(7500);
		 adr2.setVille("Puteaux");
		 adr2.setVoie("Georges Clemenceau");
		 
		 adresses.add(adr1);
		 adresses.add(adr2);
		 
		 Personne p1= new Personne();
		 p1.getAdresses().addAll(adresses);
		 p1.setAge(29);
		 p1.setNom("Bob");
		 p1.setPrenom("Marc");
		 
		 proxy.sauver(p1);
		 Collection<Personne> collection = proxy.toutLire();
		 
		 for (Personne personne : collection) {
			System.out.println(personne.getNom());
		} 
		 
		 try {
			 FileOutputStream fs = new FileOutputStream(new File("C:\\toto.pdf"));
			 fs.write(proxy.exporterPDF());
			 fs.close();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		AsyncHandler<ExporterPDFResponse> asyncHandler = new AsyncHandler<ExporterPDFResponse>() {
			
			@Override
			public void handleResponse(Response<ExporterPDFResponse> res) {
				try {
					 FileOutputStream fs = new FileOutputStream(new File("C:\\toto2.pdf"));
					 fs.write(res.get().getReturn());
					 fs.close();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
					e.printStackTrace();
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		};
		proxy.exporterPDFAsync(asyncHandler);
		 
		 try {
			proxy.lire("test");
		} catch (NomInconnuException_Exception e) {
			System.out.println("Erreur......nom inconnu");
			e.printStackTrace();
		} 
	}
	
	

}
